package newWork.controller;


import newWork.entity.ProjectEntity;
import newWork.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import newWork.entity.CommonResult;
import newWork.constant.ResultConstant;
import newWork.service.IStaffProjectService;
import newWork.entity.StaffProjectEntity;

import java.math.BigDecimal;

@RestController
@RequestMapping("/staffProject")
public class StaffProjectController {


    @Autowired
    private IStaffProjectService service;

    @Autowired
    private IProjectService projectService;

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public CommonResult select(@RequestBody StaffProjectEntity entity) {
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, service.select(entity));
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult update(@RequestBody StaffProjectEntity entity) {
        service.update(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 添加
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@RequestBody StaffProjectEntity entity) {
        service.add(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CommonResult delete(@RequestBody StaffProjectEntity entity) {
        service.delete(entity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG);
    }

    @PostMapping("/calc")
    public CommonResult calc(@RequestBody StaffProjectEntity staffProjectEntity) {
        //获取到项目id 进行查询人数以及总价格
        Integer projectId = staffProjectEntity.getProjectId();
        ProjectEntity projectEntity = new ProjectEntity();
        projectEntity.setProjectId(projectId);
        //获取到这个项目
        ProjectEntity project = projectService.select(projectEntity).get(0);
        //计算总人数
        int total = project.getRenderStaff().split(",").length +
                project.getRenderHead().split(",").length +
                project.getModelStaff().split(",").length +
                project.getModelHead().split(",").length +
                project.getLastStaff().split(",").length +
                project.getLastHead().split(",").length;
        //总价格*0.8
        BigDecimal projectTotalPrice = project.getProjectTotalPrice().multiply(new BigDecimal("0.8"));
        if (staffProjectEntity.getAttendance() == 1) {
            projectTotalPrice = projectTotalPrice.multiply(new BigDecimal(1.2));
        } else {
            projectTotalPrice = projectTotalPrice.multiply(new BigDecimal(0.8));
        }
        if (staffProjectEntity.getIsComplete() == 1) {
            projectTotalPrice = projectTotalPrice.multiply(new BigDecimal(1.2));
        } else {
            projectTotalPrice = projectTotalPrice.multiply(new BigDecimal(0.8));
        }
        BigDecimal one = projectTotalPrice.divide(new BigDecimal(total), 2, 4);

        //结果时这个员工所参与项目的总价/总人数*自己参与的次数
        service.update(staffProjectEntity);
        return new CommonResult(ResultConstant.SUCCCSS_CODE, ResultConstant.SUCCESS_MSG, one);
    }

}
